package com.wzq.upms.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.ApplicationModel;
import com.wzq.permission.client.model.admin.UserModel;
import com.wzq.permission.client.service.ApplicationService;
import com.wzq.upms.core.EasyUiData;
import com.wzq.upms.core.JsonResponse;
import com.wzq.upms.core.SessionUtils;

@Controller
@RequestMapping("application")
public class ApplicationController {
	@Autowired
	private ApplicationService applicationServiceClient;
	/**
	 * 用户页面
	 */
	private static final String APPLICATION_VIEW = "application/application";
	
	/**
	 * 用户页面
	 * @param model 模型对象
	 * @param request request对象
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/list") 
	public ModelAndView  list(ApplicationModel model,HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView(APPLICATION_VIEW);
		return mv;
	}
	
	/**
	 * 获取数据列表
	 * @param model 模型对象
	 * @param pager 分页对象
	 * @return EasyUiData
	 * @throws Exception
	 */
	@RequestMapping("/dataList") 
	@ResponseBody
	public EasyUiData<ApplicationModel>  dataList(ApplicationModel model,Pager pager) throws Exception{
		EasyUiData<ApplicationModel> data = new EasyUiData<ApplicationModel>();
		List<ApplicationModel> result = applicationServiceClient.queryList(model, pager);
		data.setTotal(applicationServiceClient.queryByCount(model));
		data.setRows(result==null?new ArrayList<ApplicationModel>():result);
		return data;
	}
	
	
	/**
	 * 添加或修改数据
	 * @param model 模型对象
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public JsonResponse add(ApplicationModel model,HttpServletRequest request) throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"保存成功");
		if(applicationServiceClient.isExistApplication(model.getApplicationKey())>0){
			response.setMsg("应用系统KEY已存在，请重新输入");
			response.setResult(JsonResponse.FAIL);
		}else{
			UserModel user = SessionUtils.getUser(request);
			model.setCreateUser(user.getUserName());
			model.setUpdateUser(user.getUserName());
			applicationServiceClient.add(model);
		}
		return response;
	}
	
	/**
	 * 添加或修改数据
	 * @param model 模型对象
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public JsonResponse edit(ApplicationModel model,HttpServletRequest request) throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"修改成功");
		UserModel user = SessionUtils.getUser(request);
		model.setUpdateTime(new Date());
		model.setCreateUser(user.getUserName());
		model.setUpdateUser(user.getUserName());
		applicationServiceClient.updateBySelective(model);
		return response;
	}
	
	/**
	 * 根据ID查找对象
	 * @param applicationId
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/getById")
	@ResponseBody
	public ApplicationModel getById(Long applicationId) throws Exception{
		ApplicationModel model = applicationServiceClient.queryById(applicationId);
		return model;
	}
	
	/**
	 * 删除数据
	 * @param userIds 主键列表
	 * @param response 
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResponse delete(Long[] applicationIds) throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"删除成功");
		applicationServiceClient.logicDelete(applicationIds);
		return response;
	}
}
